//
//  digitalViewController.m
//  JFTRequest
//
//  Created by 竣付通 on 2017/11/20.
//  Copyright © 2017年 junHaoPay. All rights reserved.
//

#import "digitalViewController.h"
#import "THPinViewController.h"
#import "HLMBProgressHUD.h"
@interface digitalViewController ()<THPinViewControllerDelegate>
@property (nonatomic, copy) NSString *correctPin;
@property (nonatomic, assign) NSUInteger remainingPinEntries;
@end

@implementation digitalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.correctPin = @"1234";
    self.remainingPinEntries = 3;
    [self showPinViewAnimated:YES];
}

- (void)showPinViewAnimated:(BOOL)animated
{
     THPinViewController *pinViewController = [[THPinViewController alloc] initWithDelegate:self];
        pinViewController.promptTitle = @"Enter PIN";
        UIColor *darkBlueColor = [UIColor colorWithRed:0.012f green:0.071f blue:0.365f alpha:1.0f];
        pinViewController.promptColor = darkBlueColor;
        pinViewController.view.tintColor = darkBlueColor;
        pinViewController.backgroundColor = [UIColor whiteColor];
        self.view.tag = THPinViewControllerContentViewTag;
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
        [self presentViewController:pinViewController animated:animated completion:nil];
}
- (NSUInteger)pinLengthForPinViewController:(THPinViewController *)pinViewController
{
    return 4;
}

- (BOOL)pinViewController:(THPinViewController *)pinViewController isPinValid:(NSString *)pin
{
    if ([pin isEqualToString:self.correctPin]) {
        return YES;
    } else {
        self.remainingPinEntries--;
        return NO;
    }
}

- (BOOL)userCanRetryInPinViewController:(THPinViewController *)pinViewController
{
    return (self.remainingPinEntries > 0);
}

- (void)incorrectPinEnteredInPinViewController:(THPinViewController *)pinViewController
{
    if (self.remainingPinEntries > 5 / 2) {
        return;
    }

    UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Incorrect PIN", @"")
                               message:(self.remainingPinEntries == 1 ?
                                        @"You can try again once." :
                                        [NSString stringWithFormat:@"You can try again %lu times.",
                                         (unsigned long)self.remainingPinEntries])
                              delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

}

- (void)pinViewControllerWillDismissAfterPinEntryWasSuccessful:(THPinViewController *)pinViewController
{
    [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"解锁成功"];
}

- (void)pinViewControllerWillDismissAfterPinEntryWasUnsuccessful:(THPinViewController *)pinViewController
{
    [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"解锁失败"];

}

- (void)pinViewControllerWillDismissAfterPinEntryWasCancelled:(THPinViewController *)pinViewController
{

}


@end
